

const StyleElementMap = new Map<string,HTMLStyleElement>();

export default class StyleMgr{
    private currentStyle:HTMLStyleElement;
    private styleKey:string;
    private createStyleElement(){
        const style = document.createElement('style'),  //创建一个style元素
            head = document.head || document.getElementsByTagName('head')[0]; //获取head元素
        style.setAttribute("type","text/css");
        head.appendChild(style); //把创建的style元素插入到head中
        StyleElementMap.set(this.styleKey,style);
        return style;
    }
    constructor(elementKey: string){
        this.styleKey = elementKey;
        this.currentStyle = StyleElementMap.get(elementKey) || this.createStyleElement();
    }
    addClass(classCode:string){
        this.currentStyle.appendChild(document.createTextNode(classCode))
    }
}